package com.ztmg.water.service.student.impl;

import com.ztmg.water.model.student.StudentTicket;
import com.ztmg.water.vo.student.StudentTicketVO;
import com.ztmg.water.dto.student.StudentTicketDTO;
import com.ztmg.water.dto.student.StudentTicketPageDTO;
import com.ztmg.common.util.resultUtils.JsonResult;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztmg.common.constant.ErrorConst;
import org.springframework.beans.BeanUtils;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import com.ztmg.water.service.student.StudentTicketService;
import com.ztmg.water.mapper.student.StudentTicketMapper;import com.ztmg.water.model.student.StudentTicketExample;

@Service
public class StudentTicketServiceImpl implements StudentTicketService{

    @Resource
    private StudentTicketMapper studentTicketMapper;
    @Override
    public JsonResult create(StudentTicketDTO dto) {
        if(null != dto){
            StudentTicket bean = new StudentTicket();
            BeanUtils.copyProperties(dto,bean);
            bean.setCreateTime(new Date());
            //后续完善
            bean.setCreater("");
            studentTicketMapper.insert(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult update(StudentTicketDTO dto) {
        if(null != dto && null != dto.getId()){
            StudentTicket bean = new StudentTicket();
            BeanUtils.copyProperties(dto,bean);
            bean.setModifyTime(new Date());
            //后续完善
            bean.setModifier("");
            studentTicketMapper.updateByPrimaryKeySelective(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<StudentTicketVO> selectById(Integer id) {
        if(null != id){
            StudentTicket bean = studentTicketMapper.selectByPrimaryKey(id);
            StudentTicketVO vo = new StudentTicketVO();
            if(null != bean){
                BeanUtils.copyProperties(bean,vo);
            }else {
                return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
            }
            return JsonResult.success(vo);
        }else{
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<StudentTicketVO> select(StudentTicketDTO dto) {
        StudentTicketExample example = new StudentTicketExample();
        StudentTicketExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        List<StudentTicket> sysUserList = studentTicketMapper.selectByExample(example);
        StudentTicketVO vo = new StudentTicketVO();
        if(!sysUserList.isEmpty()){
            BeanUtils.copyProperties(sysUserList.get(0),vo);
        }
        return JsonResult.success(vo);
    }

    @Override
    public JsonResult<PageInfo<StudentTicketVO>> selectPage(StudentTicketPageDTO dto) {
        StudentTicketExample example = new StudentTicketExample();
        StudentTicketExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<StudentTicket> beanList = studentTicketMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(beanList);
        List<StudentTicketVO> respDTOS = beanList.parallelStream().map((bean) -> {
            StudentTicketVO vo = new StudentTicketVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        pageInfo.setList(respDTOS);
        return JsonResult.success(pageInfo);
    }

    @Override
    public JsonResult deleteById(Integer id) {
        return JsonResult.success( studentTicketMapper.deleteByPrimaryKey(id));
    }

    @Override
    public JsonResult delete(StudentTicketDTO dto) {
        StudentTicketExample example = new StudentTicketExample();
        StudentTicketExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        return JsonResult.success(studentTicketMapper.deleteByExample(example));
    }
}